package info.sean.main;

import info.sean.entity.DebitBean;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;

public class DistinctKeyFiled {


    private static final List<DebitBean> DEBITBEAN = new ArrayList<>();


    static {
        for (int i = 1; i <= 12; i++) {
            DebitBean debitBean = new DebitBean();
            switch (i){
                case 1:
                    debitBean.setBiddingNo("111111111");
                    debitBean.setOverdueDay(i);
                    break;
                case 2:
                    debitBean.setBiddingNo("333333333");
                    debitBean.setOverdueDay(i);
                    break;
                case 3:
                    debitBean.setBiddingNo("333333333");
                    debitBean.setOverdueDay(0);
                    break;
                case 4:
                    debitBean.setBiddingNo("444444444");
                    debitBean.setOverdueDay(i);
                    break;
                case 5:
                    debitBean.setBiddingNo("333333333");
                    debitBean.setOverdueDay(i);
                    break;
                case 6:
                    debitBean.setBiddingNo("666666666");
                    debitBean.setOverdueDay(15);
                    break;
                case 7:
                    debitBean.setBiddingNo("777777777");
                    debitBean.setOverdueDay(-1);
                    break;
                case 8:
                    debitBean.setBiddingNo("888888888");
                    debitBean.setOverdueDay(18);
                    break;
                case 9:
                    debitBean.setBiddingNo("999999999");
                    debitBean.setOverdueDay(i);
                    break;
                case 10:
                    debitBean.setBiddingNo("000000000");
                    debitBean.setOverdueDay(i);
                    break;
                case 11:
                    debitBean.setBiddingNo("000000000");
                    debitBean.setOverdueDay(25);
                    break;
                case 12:
                    debitBean.setBiddingNo("111111111");
                    debitBean.setOverdueDay(i);
                    break;
                default:
                    break;
            }
            DEBITBEAN.add(debitBean);
        }
    }


    public static void main(String[] args) {

        List<DebitBean> debitBeans = DistinctKeyFiled.DEBITBEAN;
        for (DebitBean debitBean : debitBeans) {
            System.out.println("debitBean.getBiddingNo() = " + debitBean.getBiddingNo());
        }

        System.out.println("\"-------------------------------\" = " + "-------------------------------");

        List<DebitBean> debitBeanList = debitBeans.parallelStream().filter(distinctByKey(DebitBean::getBiddingNo)).collect(Collectors.toList());
        for (DebitBean debitBean : debitBeanList) {
            System.out.println("debitBean.getBiddingNo() = " + debitBean.getBiddingNo());

        }

        Integer minxOvdueDay = debitBeans.stream().filter(debitBean -> debitBean != null).map(DebitBean::getOverdueDay).distinct().min((e1, e2) -> e1.compareTo(e2)).get();
        Integer maxOvdueDay = debitBeans.stream().filter(debitBean -> debitBean != null).map(DebitBean::getOverdueDay).distinct().max((e1, e2) -> e1.compareTo(e2)).get();
        System.out.println("maxOvdueDay = " + maxOvdueDay);
        System.out.println("minxOvdueDay = " + minxOvdueDay);

    }



    private static <T> Predicate<T> distinctByKey(Function<? super T, ?> getBiddingNo) {
        Set<Object> seen = ConcurrentHashMap.newKeySet();
        return t -> seen.add(getBiddingNo.apply(t));
    }





}
